<template>
	<el-dialog v-model="isVisible" title="新增邀请" width="500px" :before-close="handleClose">
		<el-form ref="formRef" class="good-service-form" :model="form" :rules="rules" label-width="120px" inline>
			<el-form-item label="姓名" prop="name">
				<el-input v-model="form.name" placeholder="输入姓名" />
			</el-form-item>
			<el-form-item label="手机号" prop="phone">
				<el-input-number class="num-left" :controls="false" v-model="form.phone" placeholder="输入11位手机号" :maxlength="11" />
			</el-form-item>
			<el-form-item label="角色描述" prop="roleDesc">
				<el-input type="textarea" v-model="form.roleDesc" placeholder="输入角色描述" :maxlength="100" rows="3" show-word-limit />
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleClose">取消</el-button>
				<el-button type="primary" v-debounce="submit">保存</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="tsx">
import { FormRules, ElMessage } from 'element-plus';
import { ref, reactive, watch } from 'vue';
import { fetchCreateInvite } from '/@/api/activity';
import { verifyAccountName } from '/@/utils/toolsValidate';
import { useRoute } from 'vue-router';
const route = useRoute();

const props = defineProps({
	// 列表内容
	visible: {
		type: Boolean,
		default: () => false,
	},
	row: {
		type: Object,
		default: () => null,
	},
});

const emit = defineEmits(['handleClose']);

const isVisible = ref(false);

const formRef = ref();
const form = ref<any>({});

const validateName = (rule: any, value: any, callback: any) => {
	if (!verifyAccountName(value)) {
		callback(new Error('姓名仅支持中英文'));
	} else {
		callback();
	}
};

//校验规则
const rules = reactive<FormRules<any>>({
	name: [
		{ required: true, message: '输入姓名', trigger: 'blur' },
		{
			validator: validateName,
			message: '请输入有效的姓名',
			trigger: 'blur',
		},
	],
	phone: [
		{ required: true, message: '请输入手机号码', trigger: 'blur' },
		{ pattern: /^1[3456789]\d{9}$/, message: '请输入有效的手机号', trigger: 'blur' },
	],
});

watch(
	() => props.visible,
	(val) => {
		isVisible.value = val;
	},
	{
		immediate: true,
	}
);

const submit = async () => {
	formRef.value.validate(async (valid: boolean) => {
		if (valid) {
			const res = await fetchCreateInvite({ ...form.value, activityId: route.params.id });
			if (res.code == 200) {
				ElMessage.success('审核完成');
				handleClose(true);
			} else {
				ElMessage.error(res.msg);
			}
		}
	});
};

const handleClose = (success?: boolean) => {
	form.value = {
		order: 0,
	};
	formRef.value.resetFields();
	emit('handleClose', success === true);
};
</script>

<style lang="scss">
.good-service-form {
	.el-form-item {
		width: 90%;
		.el-input-number {
			width: 100%;
		}
	}
}
</style>
